<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    ==={{uage}}
    <div>
      <span>姓名:</span>
      <input v-model="uname" type="text" />
    </div>
    <div>
      <span>年龄:</span>
      <input v-model="uage" type="number" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="usex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="add()">添加/修改</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item,index) in arr" :key="item.id">
          <td>{{index+1}}</td>
          <td>{{item.name}}</td>
          <td>{{item.age}}</td>
          <td>{{item.sex}}</td>
          <td>
            <button @click="del(index)">删除</button>
            <button @click="edit(item.id)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.14/vue.js"></script>
  <script>
    // 需求1: 铺设页面, 准备初始的数据(自己手写数据结构) - 前面是数组索引+1 *作为序号
    // 需求2: 当输入框没有值, 要给用户一个提示, 必须都有值才能增加新数据 (数据驱动页面哦)
    // 需求3: 添加功能 - 想好数据结构统一对象的key
    // 需求4: 点击编辑功能, 把值赋予到输入框上(不要操作dom, 数据驱动页面)
    // 需求5: 用户修改后, 点击相同按钮 - 想想怎么判断怎么是添加还是修改的功能 (提示: 准备一个全局变量, 点过编辑按钮可以让它为true) - 实现编辑后更新页面效果
    // 需求6: 点击删除, 删除这行数据
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          { id: 1, name: '小觉', age: 25, sex: '男' },
          { id: 2, name: '小叹', age: 26, sex: '男' },
          { id: 3, name: '小冷', age: 27, sex: '女' },
        ],
        uname: '',
        uage: '',
        usex: '男',
        flag: true,
        uid: ''
      },
      methods: {
        //添加
        add () {
          console.log(this.uname, this.usex, this.uage);
          if (this.flag) {
            if (this.uname === '' || this.uage === '') {
              return alert('请输入内容')
            } else {
              this.arr.push({
                id: Date.now(),
                name: this.uname,
                age: this.uage,
                sex: this.usex
              })
              this.uname = this.uage = ''
              this.usex = '男'
            }
          } else {
            this.arr.forEach((item, index) => {
              if (item.id === this.uid) {
                this.arr[index].age = this.uage
                this.arr[index].name = this.uname
                this.arr[index].sex = this.usex
              }
            })
            this.flag = true

            this.uname = this.uage = ''
            this.usex = '男'
          }

        },
        //删除
        del (index) {
          this.arr.splice(index, 1)
        },
        //编辑
        edit (id) {
          this.flag = !this.flag
          this.uid = id
          // console.log(this.uid);
          this.arr.forEach((item, index) => {
            if (item.id === id) {
              this.uname = item.name
              this.usex = item.sex
              this.uage = item.age
            }
          })
        }
      }
    })
  </script>
</body>

</html>